package com.beanbean.voicechanger;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.fmod.FMOD;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private final static String TAG = "MainActivity";
    private final static int VOICE_NORMAL = 0;//原声
    private final static int VOICE_LUOLI = 1;
    private final static int VOICE_DASHU = 2;
    private final static int VOICE_JINGSONG = 3;
    private final static int VOICE_GAOGUAI = 4;
    private final static int VOICE_KONGLING = 5;

    private String voicePath;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("voice-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        voicePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separatorChar+ "poem/speech/10049.wav";
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.INTERNET,Manifest.permission.RECORD_AUDIO}, 1);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        FMOD.init(this);
        if (!new File(voicePath).exists() || !new File(voicePath).isFile()){
            Log.e(TAG,"voicePath is error,voicePath="+voicePath);
        }
    }

    public void btClick(View view) {
        switch (view.getId()){
            case R.id.bt_normal:
                changeVoice(voicePath,VOICE_NORMAL);
                break;
            case R.id.bt_luo_li:
               changeVoice(voicePath,VOICE_LUOLI);
                break;
            case R.id.bt_da_shu:
               changeVoice(voicePath,VOICE_DASHU);
                break;
            case R.id.bt_jing_song:
               changeVoice(voicePath,VOICE_JINGSONG);
                break;
            case R.id.bt_gao_guai:
               changeVoice(voicePath,VOICE_GAOGUAI);
                break;
            case R.id.bt_kong_ling:
                changeVoice(voicePath,VOICE_KONGLING);
                break;
        }
    }

    /**
     * 调用c/c++方法，变音
     * @param path 音频文件路径
     * @param voiceMode 声音模式
     */
    public native void changeVoice(String path,int voiceMode);


    @Override
    protected void onDestroy() {
        super.onDestroy();
        FMOD.close();
    }
}
